/**
 * 
 */
package com.infodms.dms.actions.sales.ordermanage.orderaudit;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.actions.sales.planmanage.QuotaAssign.ResourceReserveQuery;
import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.getCompanyId.GetOemcompanyId;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.common.VerFlagDao;
import com.infodms.dms.dao.sales.financemanage.AccountBalanceDetailDao;
import com.infodms.dms.dao.sales.ordermanage.audit.OrderAuditDao;
import com.infodms.dms.dao.sales.ordermanage.orderreport.OrderReportDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TmBusinessAreaPO;
import com.infodms.dms.po.TmBusinessParaPO;
import com.infodms.dms.po.TmDateSetPO;
import com.infodms.dms.po.TtVsDlvryReqDtlPO;
import com.infodms.dms.po.TtVsDlvryReqPO;
import com.infodms.dms.po.TtVsOrderDetailPO;
import com.infodms.dms.po.TtVsOrderPO;
import com.infodms.dms.po.TtVsOrderResourceReservePO;
import com.infodms.dms.po.TtVsReqCheckPO;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.DateTimeUtil;
import com.infodms.dms.util.businessUtil.ReqLogUtil;
import com.infodms.dms.util.csv.CsvWriterUtil;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.ActionContextExtend;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.mvc.context.ResponseWrapper;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;
import static com.infodms.dms.actions.sales.financemanage.AccountOpera.* ;

/**
 * @author Administrator
 * 
 */
public class OrderResourceReserve {
	private Logger logger = Logger.getLogger(OrderResourceReserve.class);
	private ActionContext act = ActionContext.getContext();
	RequestWrapper request = act.getRequest();
	private final OrderAuditDao dao = OrderAuditDao.getInstance();
	private final OrderReportDao reportDao = OrderReportDao.getInstance();
	private final AccountBalanceDetailDao accoutDao = AccountBalanceDetailDao.getInstance();

	private final String ORDER_RESOURCE_RESERVE_QUERY_URL = "/jsp/sales/ordermanage/orderaudit/orderResourceReserveQuery.jsp";// 订单资源审核查询页
	private final String ORDER_RESOURCE_RESERVE_DETAIL_QUERY_URL = "/jsp/sales/ordermanage/orderaudit/orderResourceReserveDetail.jsp";// 订单资源审核明细页
	private final String ORDER_RESOURCE_RESERVE_PATCH_NO_QUERY_URL = "/jsp/sales/ordermanage/orderaudit/orderResourceReservePatchNoQuery.jsp";// 订单资源审核批次号选择页

	public void orderResourceReserveQueryPre() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			Long oemCompanyId = GetOemcompanyId.getOemCompanyId(logonUser);
			
			TmDateSetPO dateSet = reportDao.getTmDateSetPO(new java.util.Date(), oemCompanyId);
			String dateStr = DateTimeUtil.parseDateToDate(new Date());
			act.setOutData("dateStr", dateStr);
			
			List<Map<String, Object>> dateList = dao.getNastyDateList(oemCompanyId);
			List<Map<String, Object>> areaBusList = MaterialGroupManagerDao
					.getPoseIdBusiness(logonUser.getPoseId().toString());
			String duty = logonUser.getDutyType();
			String orgId = logonUser.getOrgId().toString();
			if (Constant.DUTY_TYPE_DEPT.toString().equals(duty)) {
				orgId = logonUser.getParentOrgId();
			}
			// 回显查询条件
			String dealerCode = request.getParamValue("dealerCode"); // 经销商CODE
			String areaId = request.getParamValue("areaId"); // 业务范围ID
			String groupCode = request.getParamValue("groupCode"); // 物料组CODE
			String orderType = request.getParamValue("orderType"); // 订单类型
			String orderNo = request.getParamValue("orderNo"); // 订单号码
			String reqStatus = request.getParamValue("reqStatus"); // 状态
			String orgCode = request.getParamValue("orgCode");// 区域代码
			// 获得是否需要资金检查
			TmBusinessParaPO para = dao.getTmBusinessParaPO(
					Constant.URGENT_ORDER_ACCOUNT_CHECK_PARA, oemCompanyId);
			String isCheck = para.getParaValue();
			act.setOutData("dateList", dateList);
			act.setOutData("areaBusList", areaBusList);
			act.setOutData("isCheck", isCheck);
			act.setOutData("orgId", orgId);
			act.setOutData("dealerCode", dealerCode);
			act.setOutData("areaId", areaId);
			act.setOutData("groupCode", groupCode);
			act.setOutData("orderType", orderType);
			act.setOutData("orderNo", orderNo);
			act.setOutData("reqStatus", reqStatus);
			act.setOutData("orgCode", orgCode);
			act.setForword(ORDER_RESOURCE_RESERVE_QUERY_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE,
					"订单资源审核查询");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	/**
	 * 订单资源审核查询
	 */
	public void orderResourceReserveQuery() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			String areaId = request.getParamValue("areaId"); // 业务范围ID
			String area = request.getParamValue("area"); // 业务范围IDS
			String dealerCode = request.getParamValue("dealerCode"); // 经销商CODE
			String groupCode = request.getParamValue("groupCode"); // 物料组CODE
			String orderType = request.getParamValue("orderType"); // 订单类型
			String orderNo = request.getParamValue("orderNo"); // 订单号码
			String reqStatus = request.getParamValue("reqStatus"); // 状态
			// String orgCode = request.getParamValue("orgCode");// 区域代码
			String orgIdN = request.getParamValue("orgId");// 区域代码
			String beginTime=request.getParamValue("startDate");//开始日期
			String endTime=request.getParamValue("endDate");//结束日期
			
			Long companyId = GetOemcompanyId.getOemCompanyId(logonUser);
			String dutyType = logonUser.getDutyType();
			String orgId = "" ;
			
			if(Constant.DUTY_TYPE_LARGEREGION.toString().equalsIgnoreCase(dutyType)) {
				orgId = logonUser.getOrgId().toString();
			} else {
				if(!CommonUtils.isNullString(orgIdN)) {
					orgId = orgIdN ;
				}
			}

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("areaId", areaId);
			map.put("area", area);
			map.put("groupCode", groupCode);
			map.put("dealerCode", dealerCode);
			map.put("orderType", orderType);
			map.put("orderNo", orderNo);
			map.put("companyId", companyId.toString());
			map.put("reqStatus", reqStatus);
			// map.put("orgCode", orgCode);
			map.put("dutyType", dutyType);
			map.put("orgId", orgId);
			map.put("beginTime", beginTime);
			map.put("endTime", endTime);
			Integer curPage = request.getParamValue("curPage") != null ? Integer.parseInt(request
					.getParamValue("curPage")) : 1;
			PageResult<Map<String, Object>> ps = dao.getOrderResourceReserveQuery(map, curPage,
					Constant.PAGE_SIZE);
			act.setOutData("ps", ps);
			act.setOutData("orderType", orderType);
		} catch (Exception e) {// 异常方法
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE,
					"订单资源审核查询");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	public void orderResourceReserveQueryLoad() {
		OutputStream os = null;
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			String areaId = request.getParamValue("areaId"); // 业务范围ID
			String area = request.getParamValue("area"); // 业务范围IDS
			String dealerCode = request.getParamValue("dealerCode"); // 经销商CODE
			String groupCode = request.getParamValue("groupCode"); // 物料组CODE
			String orderType = request.getParamValue("orderType"); // 订单类型
			String orderNo = request.getParamValue("orderNo"); // 订单号码
			String reqStatus = request.getParamValue("reqStatus"); // 状态
			String orgCode = request.getParamValue("orgCode");// 区域代码
			String beginTime=request.getParamValue("startDate");//开始日期
			String endTime=request.getParamValue("endDate");//结束日期
			
			Long companyId = GetOemcompanyId.getOemCompanyId(logonUser);
			String dutyType = logonUser.getDutyType();
			String orgId = logonUser.getOrgId().toString();

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("areaId", areaId);
			map.put("area", area);
			map.put("groupCode", groupCode);
			map.put("dealerCode", dealerCode);
			map.put("orderType", orderType);
			map.put("orderNo", orderNo);
			map.put("companyId", companyId.toString());
			map.put("reqStatus", reqStatus);
			map.put("orgCode", orgCode);
			map.put("dutyType", dutyType);
			map.put("orgId", orgId);
			map.put("beginTime", beginTime);
			map.put("endTime", endTime);
			Integer curPage = request.getParamValue("curPage") != null ? Integer.parseInt(request.getParamValue("curPage")) : 1;
			//PageResult<Map<String, Object>> ps = dao.getOrderResourceReserveLoadQuery(map, curPage,999999);
			PageResult<Map<String, Object>> ps = dao.getOrderResourceReserveDetailDownLoad(map, curPage, 999999);
			ResponseWrapper response = act.getResponse();
			// 导出的文件名
			String fileName = "资源审核下载.csv";
			// 导出的文字编码
			fileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
			response.setContentType("Application/text/csv");
			response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
			// 定义一个集合
			List<List<Object>> list = new LinkedList<List<Object>>();
			// 标题
			List<Object> listTemp = new LinkedList<Object>();
			listTemp.add("开票单位代码");
			listTemp.add("开票单位名称");
			listTemp.add("采购单位名称");
			listTemp.add("订单号码");
			listTemp.add("发运申请单号");
			listTemp.add("提报日期");
			listTemp.add("订单类型");
			listTemp.add("状态");
			listTemp.add("申请数量");
			listTemp.add("已保留数量");
			listTemp.add("资金类型");
			listTemp.add("订单总价");
			listTemp.add("车型编码");
			list.add(listTemp);
			List<Map<String, Object>> rslist = ps.getRecords();
			if(rslist!=null){
			for (int i = 0; i < rslist.size(); i++) {
				map = rslist.get(i);
				List<Object> listValue = new LinkedList<Object>();
				listValue = new LinkedList<Object>();
				listValue.add(map.get("DEALER_CODE") != null ? map.get("DEALER_CODE") : "");
				listValue.add(map.get("DEALER_NAME") != null ? map.get("DEALER_NAME") : "");
				listValue.add(map.get("ORDER_DEALER_NAME") != null ? map.get("ORDER_DEALER_NAME") : "");
				listValue.add(map.get("ORDER_NO") != null ? map.get("ORDER_NO") : "");
				listValue.add(map.get("DLVRY_REQ_NO") != null ? map.get("DLVRY_REQ_NO") : "");
				listValue.add(map.get("RAISE_DATE") != null ? map.get("RAISE_DATE") : "");
				listValue.add(map.get("ORDER_TYPE") != null ? map.get("ORDER_TYPE") : "");
				listValue.add(map.get("REQ_STATUS") != null ? map.get("REQ_STATUS") : "");
				listValue.add(map.get("ORDER_AMOUNT") != null ? map.get("ORDER_AMOUNT") : "");
				listValue.add(map.get("RESERVE_AMOUNT") != null ? map.get("RESERVE_AMOUNT") : "");
				listValue.add(map.get("FUND_TYPE") != null ? map.get("FUND_TYPE") : "");
				listValue.add(map.get("MY_PRICE") != null ? map.get("MY_PRICE") : "");
				listValue.add(map.get("CO") != null ? map.get("CO") : "");
				list.add(listValue);
			}
			}
			os = response.getOutputStream();
			CsvWriterUtil.writeCsv(list, os);
			os.flush();
		} catch (Exception e) {// 异常方法
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE,
					"资源审核下载");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	
	
	
	

	/**
	 * 订单资源审核明细
	 */
	public void orderResourceReserveDetailQuery() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			
			ActionContextExtend atx = (ActionContextExtend) ActionContext.getContext();
			String reqURL = atx.getRequest().getContextPath();
			if("/CVS-SALES".equals(reqURL.toUpperCase())){
				act.setOutData("returnValue", 1);
			}else{
				act.setOutData("returnValue", 2);
			}
			
			String orderId = request.getParamValue("orderId"); // 订单ID
			String reqId = request.getParamValue("reqId"); // 发车申请ID
			String orderYearWeek = request.getParamValue("orderYearWeek"); // 订单年周
			String dealerCode = request.getParamValue("dealerCode"); // 经销商CODE
			String areaId = request.getParamValue("areaId"); // 业务范围ID
			String groupCode = request.getParamValue("groupCode"); // 物料组CODE
			String orderType = request.getParamValue("orderType"); // 订单类型
			String orderNo = request.getParamValue("orderNo"); // 订单号码
			String reqStatus = request.getParamValue("reqStatus"); // 状态
			String orgCode = request.getParamValue("orgCode");// 区域代码
			String historyCount = request.getParamValue("historyCount") ;

			Map<String, Object> map = dao.getOrderInfoByReqId(reqId);
			String areaGet = ((BigDecimal) map.get("AREA_ID")).toString();
			TmBusinessAreaPO areaPO = new TmBusinessAreaPO();
			areaPO.setAreaId(new Long(areaGet));
			List<PO> areaList = dao.select(areaPO);
			Long erpCode = null;
			if(areaList.size() != 0){
				areaPO = (TmBusinessAreaPO)areaList.get(0);
				erpCode = areaPO.getErpCode();
			}
			
			List<Map<String, Object>> list1 = dao.getorderResourceReserveDetailList(reqId,
					orderType, logonUser.getCompanyId().toString(), erpCode.toString());
			List<Map<String, Object>> list2 = dao.getReqCheckList(reqId);
			List<Map<String, Object>> wareHouseList = dao.getWareHouseList(logonUser.getCompanyId()
					.toString(), areaGet);// 仓库列表 
			TmBusinessParaPO para = dao.getTmBusinessParaPO(
					Constant.RESOURCE_RESERVE_CHECK_GENERAL_ORDER_PARA, logonUser.getCompanyId());
			// 获得订单启票最大折扣点
			TmBusinessParaPO para2 = dao.getTmBusinessParaPO(
					Constant.ORDER_REPORT_MAX_DISCOUNT_RATE_PARA, logonUser.getCompanyId());
			String ratePara = para2.getParaValue();

			act.setOutData("map",map);
			act.setOutData("orderId", orderId);
			act.setOutData("reqId", reqId);
			act.setOutData("list1", list1);
			act.setOutData("list2", list2);
			act.setOutData("orderYearWeek", orderYearWeek);
			act.setOutData("dealerCode", dealerCode);
			act.setOutData("areaId", areaId);
			act.setOutData("groupCode", groupCode);
			act.setOutData("orderType", orderType);
			act.setOutData("orderNo", orderNo);
			act.setOutData("reqStatus", reqStatus);
			act.setOutData("orgCode", orgCode);
			act.setOutData("wareHouseList", wareHouseList);
			act.setOutData("checkGeneral", para != null ? para.getParaValue() : "0");
			act.setOutData("ratePara", ratePara);
			act.setOutData("erpCode", erpCode);
			act.setOutData("historyCount", historyCount) ;

			act.setForword(ORDER_RESOURCE_RESERVE_DETAIL_QUERY_URL);
		} catch (Exception e) {// 异常方法
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE,
					"订单资源审核查询");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	/**
	 * 
	 */
	public void orderResourceReserveSave() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			String[] materialId = request.getParamValues("materialId");							// 物料ID
			String[] reqAmount = request.getParamValues("reqAmount");							// 申请数量
			String[] reserveAmount = request.getParamValues("reserveAmount");					// 保留数量
			String[] singlePrice = request.getParamValues("singlePrice");						// 单价
			String[] discountRate = request.getParamValues("discountRate");						// 折扣率
			String[] totalPrice = request.getParamValues("totalPrice");							// 金额
			String[] discountSPrice = request.getParamValues("discountSPrice");					// 折扣后单价
			String[] discountPrice = request.getParamValues("discountPrice");					// 折扣额
			String[] batchNo = request.getParamValues("batchNo");								// 批次号
			String reqVer = request.getParamValue("reqVer");									// 发运申请VER
			String[] ver = request.getParamValues("ver");										// 发运申请明细VER
			String orderId = request.getParamValue("orderId"); 									// 订单ID
			String reqId = request.getParamValue("reqId"); 										// 发运申请ID
			String[] detailId = request.getParamValues("detailId");								// 发运申请明细ID
			String[] orderDetailId = request.getParamValues("orderDetailId");					// 订单明细ID
			String orderType = request.getParamValue("orderType"); 								// 订单类型
			String warehouseId = request.getParamValue("warehouseId"); 							// 仓库ID
			String fundTypeId = request.getParamValue("fundType"); 								// 资源类型
			String priceId = request.getParamValue("priceId"); 									// 价格类型
			String otherPriceReason = request.getParamValue("otherPriceReason"); 				// 使用其他价格原因
			String dealerId = request.getParamValue("dealerId"); 								// 开票方id
			String checkRemark = request.getParamValue("checkRemark"); 							// 审核描述
			String reqTotalPrice = request.getParamValue("reqTotalPrice"); 						// 总价(折扣后)
			String discountTotalPrice = request.getParamValue("discountTotalPrice"); 			// 折扣总额
			String modifyFlag = request.getParamValue("modifyFlag"); 							// 是否修改标志
			String reserveTotalAmount = request.getParamValue("reserveTotalAmount"); 			// 保留数量合计
			String accountId = CommonUtils.checkNull(request.getParamValue("accountId"));		// 资金账户
			String discountAccountId = CommonUtils.checkNull(request.getParamValue("discountAccountId"));// 折扣账户
			String deliveryType = CommonUtils.checkNull(request.getParamValue("deliveryType"));	// 发运方式
			String orderRemark = CommonUtils.checkNull(request.getParamValue("orderRemark"));	// 备注
			// String receiver =
			// CommonUtils.checkNull(request.getParamValue("receiver")); // 收货方
			// String linkMan =
			// CommonUtils.checkNull(request.getParamValue("linkMan")); // 联系人
			// String tel = CommonUtils.checkNull(request.getParamValue("tel"));
			// // 联系电话
			
			String isBingcai = CommonUtils.checkNull(request.getParamValue("isBingcai")); 		//资金类型:1=兵财存货融资; 0=非兵财存货融资
			String old_fund_type_id = CommonUtils.checkNull(request.getParamValue("old_fund_type_id")); //原资金类型id
			
			String returnValue = "1";
			boolean verFlag = true;
			verFlag = VerFlagDao.getVerFlag("TT_VS_DLVRY_REQ", "REQ_ID", reqId, reqVer);
			if (verFlag) {
				/*
				 * for (int m = 0; m < detailId.length; m++) { if (detailId[m] !=
				 * null && !detailId[m].equals("")) { boolean verFlagTmp =
				 * VerFlagDao.getVerFlag("TT_VS_DLVRY_REQ_DTL", "DETAIL_ID",
				 * detailId[m], ver[m]); if (verFlagTmp == false) { verFlag =
				 * false; } } }
				 */
				if (verFlag) {
					TtVsOrderPO orderPo = reportDao.getTtSalesOrder(orderId);// 订单po

					/*if(!"1".equals(isBingcai)){
						// 资金校验(如果 资金类型:1=兵财存货融资，不做资金校验)
						Map<String, Object> accountMap = reportDao.getAvailableAmount(fundTypeId.split("\\|")[0],
								dealerId, reqId);
						BigDecimal availableAmount = (BigDecimal) accountMap.get("AVAILABLE_AMOUNT");
						if (availableAmount.doubleValue() < Double.parseDouble(reqTotalPrice)) {
							returnValue = "3";
						}
					}*/
					

					// 校验通过，冻结资金
					if (returnValue.equals("1")) {
						//查看原资金类型是否是“兵财存货融资”(如果IS_USE_ORDER_ACCOUNT == 1，说明是"兵财存货融资")
						Map<String,String> oldFund = accoutDao.oldIsCaibing(old_fund_type_id);
						String old_isCaibing = String.valueOf(oldFund.get("IS_USE_ORDER_ACCOUNT"));
						
						/*// 资金释放
						if (!"1".equals(old_isCaibing) && "1".equals(isBingcai)) {
							
						}*/
						dmsReleasePrice(reqId.toString(), logonUser.getUserId().toString()) ;
						// 冻结资金(如果 资金类型:1=兵财存货融资，不做资金同步)
						if (!accountId.equals("") && !"1".equals(isBingcai)) {
							dmsFreezePrice(reqId, accountId, new BigDecimal(reqTotalPrice), logonUser.getUserId().toString());
						}
						if (!discountAccountId.equals("") && !"1".equals(isBingcai)) {
							dmsFreezePrice(reqId, discountAccountId, new BigDecimal(discountTotalPrice), logonUser.getUserId().toString());
						}
						
						/*if (!"1".equals(old_isCaibing) && "1".equals(isBingcai)) {
							AccountBalanceDetailDao dao = AccountBalanceDetailDao.getInstance();
							dao.releaseAllFreezeAmountByReqId(reqId, logonUser.getUserId().toString());
						}*/
						
						/*// 冻结资金(如果 资金类型:1=兵财存货融资，不做资金同步)
						if (!accountId.equals("") && !"1".equals(isBingcai)) {
							accoutDao.syncAccountFreeze(reqId, accountId, new BigDecimal(reqTotalPrice), logonUser.getUserId().toString());
						}
						if (!discountAccountId.equals("") && !"1".equals(isBingcai)) {
							accoutDao.syncAccountFreeze(reqId, discountAccountId, new BigDecimal(discountTotalPrice), logonUser.getUserId().toString());
						}*/

						// 更新订单表
						TtVsOrderPO tvop1 = new TtVsOrderPO();
						TtVsOrderPO tvop2 = new TtVsOrderPO();
						tvop1.setOrderId(Long.parseLong(orderId));
						tvop2.setVer(orderPo.getVer().intValue() + 1);
						tvop2.setFundTypeId(new Long(fundTypeId.split("\\|")[0])) ;
						// tvop2.setOrderRemark(orderRemark);
						tvop2.setOrderStatus(Constant.ORDER_STATUS_05);
						tvop2.setUpdateBy(logonUser.getUserId());
						tvop2.setUpdateDate(new Date(System.currentTimeMillis()));
						dao.update(tvop1, tvop2);

						// 更新发运申请表
						TtVsDlvryReqPO tvdrp1 = new TtVsDlvryReqPO();
						TtVsDlvryReqPO tvdrq2 = new TtVsDlvryReqPO();
						tvdrp1.setReqId(new Long(reqId));
						tvdrq2.setFundType(new Long(fundTypeId.split("\\|")[0]));
						tvdrq2.setReqRemark(orderRemark) ;
						if(priceId != null && !priceId.equals("")){
							tvdrq2.setPriceId(new Long(priceId));
						}
						tvdrq2.setOtherPriceReason(otherPriceReason);
						tvdrq2.setModifyFlag(new Integer(modifyFlag));
						tvdrq2.setWarehouseId(new Long(warehouseId));
						if (modifyFlag.equals("1")) {
							tvdrq2.setReqStatus(Constant.ORDER_REQ_STATUS_02);// 经销商待确认
						} else {
							tvdrq2.setReqStatus(Constant.ORDER_REQ_STATUS_03);// 初审完成
						}
						tvdrq2.setReqTotalPrice(new Double(reqTotalPrice));
						tvdrq2.setDiscount(new Double(discountTotalPrice));
						tvdrq2.setReserveTotalAmount(new Integer(reserveTotalAmount));
						tvdrq2.setDeliveryType(new Integer(deliveryType));
						// 发运方式为自提时
						if (tvdrq2.getDeliveryType().intValue() == Constant.TRANSPORT_TYPE_01.intValue()) {
							tvdrq2.setReceiver(orderPo.getOrderOrgId());
						}
						// 发运方式为发运时
						/*
						 * else { tvdrq2.setReceiver(new Long(receiver));
						 * tvdrq2.setLinkMan(linkMan); tvdrq2.setTel(tel); }
						 */
						tvdrq2.setVer(new Integer(Integer.parseInt(reqVer) + 1));
						tvdrq2.setUpdateBy(logonUser.getUserId());
						tvdrq2.setUpdateDate(new Date(System.currentTimeMillis()));
						if (Integer.parseInt(orderType) == Constant.ORDER_TYPE_01.intValue()) {
							tvdrq2.setReqTotalAmount(new Integer(reserveTotalAmount));
						}
						dao.update(tvdrp1, tvdrq2);

						// orderPo = reportDao.getTtSalesOrder(orderId);
						
						ResourceReserveQuery rrq = new ResourceReserveQuery() ;
						Long logId = rrq.insLog(reqId, logonUser.getUserId()) ;

						for (int i = 0; i < detailId.length; i++) {
							if (detailId[i] != null && !detailId[i].equals("")) {

								// 更新订单明细
								TtVsOrderDetailPO tvodp1 = new TtVsOrderDetailPO();
								TtVsOrderDetailPO tvodp2 = new TtVsOrderDetailPO();
								tvodp1.setDetailId(new Long(orderDetailId[i]));
								if (Integer.parseInt(orderType) == Constant.ORDER_TYPE_01.intValue()) {
									TtVsOrderDetailPO tvodp = reportDao.getTtSalesOrderDetail(orderDetailId[i]);
									tvodp2.setCallAmount(tvodp.getCallAmount() - new Integer(reqAmount[i]) + new Integer(reserveAmount[i]));
								} else {
									tvodp2.setCheckAmount(new Integer(reserveAmount[i]));
								}
								dao.update(tvodp1, tvodp2);

								// 更新发车申请明细
								TtVsDlvryReqDtlPO tvdrdp1 = new TtVsDlvryReqDtlPO();
								TtVsDlvryReqDtlPO tvdrdp2 = new TtVsDlvryReqDtlPO();
								tvdrdp1.setDetailId(new Long(detailId[i]));
								tvdrdp2.setSinglePrice(new Double(singlePrice[i]));
								tvdrdp2.setTotalPrice(new Double(totalPrice[i]) - new Double(discountPrice[i]));
								tvdrdp2.setDiscountRate(new Float(discountRate[i]));
								tvdrdp2.setDiscountSPrice(new Double(discountSPrice[i]));
								tvdrdp2.setDiscountPrice(new Double(discountPrice[i]));
								tvdrdp2.setReserveAmount(new Integer(reserveAmount[i]));
								tvdrdp2.setUpdateBy(logonUser.getUserId());
								tvdrdp2.setUpdateDate(new Date(System.currentTimeMillis()));
								tvdrdp2.setVer(new Integer(Integer.parseInt(ver[i] == null || ver[i].equals("") ? "0" : ver[i]) + 1));
								if (Integer.parseInt(orderType) == Constant.ORDER_TYPE_01.intValue()) {
									tvdrdp2.setReqAmount(new Integer(reserveAmount[i]));
								}
								dao.update(tvdrdp1, tvdrdp2);

								// 删除资源保留
								TtVsOrderResourceReservePO tvorrp = new TtVsOrderResourceReservePO();
								tvorrp.setReqDetailId(new Long(detailId[i]));
								dao.delete(tvorrp);

								// 新增资源保留
								if (batchNo[i] != null && !batchNo[i].equals("")) {
									String[] tempArray = batchNo[i].split("\\/");
									for (int j = 0; j < tempArray.length; j++) {
										String tempStr = tempArray[j];
										String[] tempArray2 = tempStr.split("-");
										String patchNO = tempArray2[0];
										String count = tempArray2[1];
										tvorrp = new TtVsOrderResourceReservePO();
										tvorrp.setReserveId(Long.parseLong(SequenceManager.getSequence("")));
										tvorrp.setReqDetailId(new Long(detailId[i]));
										tvorrp.setMaterialId(new Long(materialId[i]));
										tvorrp.setBatchNo(patchNO);
										tvorrp.setAmount(new Integer(count));
										tvorrp.setReserveStatus(Constant.RESOURCE_RESERVE_STATUS_01);
										tvorrp.setOemCompanyId(logonUser.getCompanyId());
										tvorrp.setWarehouseId(new Long(warehouseId));
										tvorrp.setReserveType(Constant.RESERVE_TYPE_01);
										tvorrp.setCreateBy(logonUser.getUserId());
										tvorrp.setCreateDate(new Date());
										dao.insert(tvorrp);
										
										rrq.insDtlLog(logId, detailId[i], patchNO, count, "0", materialId[i], logonUser.getUserId()) ;
									}
								}

							} else {

								// 新增订单明细
								TtVsOrderDetailPO tvodp = new TtVsOrderDetailPO();
								tvodp.setDetailId(Long.parseLong(SequenceManager.getSequence("")));
								tvodp.setOrderId(new Long(orderId));
								tvodp.setMaterialId(new Long(materialId[i]));
								tvodp.setOrderAmount(new Integer(0));
								tvodp.setCallAmount(new Integer(0));
								tvodp.setCheckAmount(new Integer(reserveAmount[i]));
								tvodp.setSinglePrice(new Double(singlePrice[i]));
								tvodp.setTotalPrice(new Double(totalPrice[i]) - new Double(discountPrice[i]));
								tvodp.setVer(new Integer(0));
								tvodp.setDiscountRate(new Float(discountRate[i]));
								tvodp.setDiscountSPrice(new Double(discountSPrice[i]));
								tvodp.setDiscountPrice(new Double(discountPrice[i]));
								tvodp.setCreateBy(logonUser.getUserId());
								tvodp.setCreateDate(new Date(System.currentTimeMillis()));
								dao.insert(tvodp);

								// 新增发车申请明细
								TtVsDlvryReqDtlPO tvdrdp = new TtVsDlvryReqDtlPO();
								tvdrdp.setDetailId(Long.parseLong(SequenceManager.getSequence("")));
								tvdrdp.setReqId(new Long(reqId));
								tvdrdp.setOrderDetailId(tvodp.getDetailId());
								tvdrdp.setMaterialId(new Long(materialId[i]));
								tvdrdp.setReqAmount(new Integer(0));
								tvdrdp.setVer(new Integer(0));
								tvdrdp.setSinglePrice(new Double(singlePrice[i]));
								tvdrdp.setTotalPrice(new Double(totalPrice[i]) - new Double(discountPrice[i]));
								tvdrdp.setDiscountRate(new Float(discountRate[i]));
								tvdrdp.setDiscountSPrice(new Double(discountSPrice[i]));
								tvdrdp.setDiscountPrice(new Double(discountPrice[i]));
								tvdrdp.setReserveAmount(new Integer(reserveAmount[i]));
								tvdrdp.setCreateBy(logonUser.getUserId());
								tvdrdp.setCreateDate(new Date(System.currentTimeMillis()));
								dao.insert(tvdrdp);

								// 新增资源保留
								if (batchNo[i] != null && !batchNo[i].equals("")) {
									String[] tempArray = batchNo[i].split("\\/");
									for (int j = 0; j < tempArray.length; j++) {
										String tempStr = tempArray[j];
										String[] tempArray2 = tempStr.split("-");
										String patchNO = tempArray2[0];
										String count = tempArray2[1];
										TtVsOrderResourceReservePO tvorrp = new TtVsOrderResourceReservePO();
										tvorrp.setReserveId(Long.parseLong(SequenceManager.getSequence("")));
										tvorrp.setReqDetailId(tvdrdp.getDetailId());
										tvorrp.setMaterialId(new Long(materialId[i]));
										tvorrp.setBatchNo(patchNO);
										tvorrp.setAmount(new Integer(count));
										tvorrp.setReserveStatus(Constant.RESOURCE_RESERVE_STATUS_01);
										tvorrp.setOemCompanyId(logonUser.getCompanyId());
										tvorrp.setWarehouseId(new Long(warehouseId));
										tvorrp.setReserveType(Constant.RESERVE_TYPE_01);
										dao.insert(tvorrp);
										
										rrq.insDtlLog(logId, tvdrdp.getDetailId().toString(), patchNO, count, "0", materialId[i], logonUser.getUserId()) ;
									}
								}
							}
						}

						//向发运申请操作日志表写入日志信息
						ReqLogUtil.creatReqLog(new Long(reqId), Constant.REQ_LOG_TYPE_02, logonUser.getUserId());
						// 新增发车申请审核记录
						TtVsReqCheckPO tvrcp = new TtVsReqCheckPO();
						tvrcp.setCheckId(Long.parseLong(SequenceManager.getSequence("")));
						tvrcp.setReqId(new Long(reqId));
						tvrcp.setCheckOrgId(logonUser.getOrgId());
						tvrcp.setCheckPositionId(logonUser.getPoseId());
						tvrcp.setCheckUserId(logonUser.getUserId());
						tvrcp.setCheckDate(new Date(System.currentTimeMillis()));
						tvrcp.setCheckStatus(Constant.CHECK_STATUS_03);
						if (checkRemark != null && !"".equals(checkRemark)) {
							tvrcp.setCheckDesc(checkRemark);
						}
						tvrcp.setCreateBy(logonUser.getUserId());
						tvrcp.setCreateDate(new Date(System.currentTimeMillis()));
						dao.insert(tvrcp);
					}

				} else {
					returnValue = "2";
				}
			} else {
				returnValue = "2";
			}
			act.setOutData("returnValue", returnValue);
		} catch(RuntimeException e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.PUTIN_FAILURE_CODE,e.getMessage());
			logger.error(logonUser,e1);
			act.setException(e);
		} catch (Exception e) {// 异常方法
			BizException e1 = new BizException(act, e, ErrorCodeConstant.SAVE_FAILURE_CODE,
					"订单资源审核保存");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	/**
	 * 订单资源审核：保留数量确认
	 */
	public void reserveAmountConfirm() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			String[] materialId = request.getParamValues("materialId");// 物料ID
			String[] reqAmount = request.getParamValues("reqAmount");// 申请数量
			String[] reserveAmount = request.getParamValues("reserveAmount");// 保留数量
			String[] singlePrice = request.getParamValues("singlePrice");// 单价
			String[] discountRate = request.getParamValues("discountRate");// 折扣率
			String[] totalPrice = request.getParamValues("totalPrice");// 金额
			String[] discountSPrice = request.getParamValues("discountSPrice");// 折扣后单价
			String[] discountPrice = request.getParamValues("discountPrice");// 折扣额
			String[] batchNo = request.getParamValues("batchNo");// 批次号
			String reqVer = request.getParamValue("reqVer");// 发运申请VER
			String[] ver = request.getParamValues("ver");// 发运申请明细VER
			String orderId = request.getParamValue("orderId"); // 订单ID
			String reqId = request.getParamValue("reqId"); // 发运申请ID
			String[] detailId = request.getParamValues("detailId");// 发运申请明细ID
			String[] orderDetailId = request.getParamValues("orderDetailId");// 订单明细ID
			String orderType = request.getParamValue("orderType"); // 订单类型
			String warehouseId = request.getParamValue("warehouseId"); // 仓库ID
			String fundTypeId = request.getParamValue("fundType"); // 资源类型
			String priceId = request.getParamValue("priceId"); // 价格类型
			String otherPriceReason = request.getParamValue("otherPriceReason"); // 使用其他价格原因
			String dealerId = request.getParamValue("dealerId"); // 开票方id
			// String checkRemark = request.getParamValue("checkRemark"); //
			// 审核描述
			String reqTotalPrice = request.getParamValue("reqTotalPrice"); // 总价(折扣后)
			String discountTotalPrice = request.getParamValue("discountTotalPrice"); // 折扣总额
			String modifyFlag = request.getParamValue("modifyFlag"); // 是否修改标志
			String reserveTotalAmount = request.getParamValue("reserveTotalAmount"); // 保留数量合计
			String accountId = CommonUtils.checkNull(request.getParamValue("accountId"));// 资金账户
			String discountAccountId = CommonUtils.checkNull(request
					.getParamValue("discountAccountId"));// 折扣账户
			String deliveryType = CommonUtils.checkNull(request.getParamValue("deliveryType"));// 发运方式
			String orderRemark = CommonUtils.checkNull(request.getParamValue("orderRemark"));// 备注
			// String receiver =
			// CommonUtils.checkNull(request.getParamValue("receiver")); // 收货方
			// String linkMan =
			// CommonUtils.checkNull(request.getParamValue("linkMan")); // 联系人
			// String tel = CommonUtils.checkNull(request.getParamValue("tel"));
			// // 联系电话

			String isBingcai = CommonUtils.checkNull(request.getParamValue("isBingcai")); //资金类型:1=兵财存货融资; 0=非兵财存货融资
			
			String returnValue = "1";

			boolean verFlag = true;
			verFlag = VerFlagDao.getVerFlag("TT_VS_DLVRY_REQ", "REQ_ID", reqId, reqVer);
			if (verFlag) {

				if (verFlag) {
					TtVsOrderPO orderPo = reportDao.getTtSalesOrder(orderId);// 订单po

					/*// 资金校验
					if (!"1".equals(isBingcai)) {
						Map<String, Object> accountMap = reportDao.getAvailableAmount(fundTypeId.split("\\|")[0],dealerId, reqId);
						BigDecimal availableAmount = (BigDecimal) accountMap.get("AVAILABLE_AMOUNT");
						if (availableAmount.doubleValue() < Double.parseDouble(reqTotalPrice)) {
							returnValue = "3";
						}
					}*/

					// 校验通过，冻结资金
					if (returnValue.equals("1")) {
						dmsReleasePrice(reqId.toString(), logonUser.getUserId().toString()) ;
						// 冻结资金(资金类型:1=兵财存货融资,不做资金同步)
						if (!accountId.equals("") && !"1".equals(isBingcai)) {
							dmsFreezePrice(reqId, accountId, new BigDecimal(reqTotalPrice), logonUser.getUserId().toString());
						}
						if (!discountAccountId.equals("") && !"1".equals(isBingcai)) {
							dmsFreezePrice(reqId, discountAccountId, new BigDecimal(discountTotalPrice), logonUser.getUserId().toString());
						}
						/*// 冻结资金(资金类型:1=兵财存货融资,不做资金同步)
						if (!accountId.equals("") && !"1".equals(isBingcai)) {
							accoutDao.syncAccountFreeze(reqId, accountId, new BigDecimal(reqTotalPrice), logonUser.getUserId().toString());
						}
						if (!discountAccountId.equals("") && !"1".equals(isBingcai)) {
							accoutDao.syncAccountFreeze(reqId, discountAccountId, new BigDecimal(discountTotalPrice), logonUser.getUserId().toString());
						}*/

						// 更新订单表
						TtVsOrderPO tvop1 = new TtVsOrderPO();
						TtVsOrderPO tvop2 = new TtVsOrderPO();
						tvop1.setOrderId(Long.parseLong(orderId));
						tvop2.setVer(orderPo.getVer().intValue() + 1);
						tvop2.setFundTypeId(new Long(fundTypeId.split("\\|")[0])) ;
						// tvop2.setOrderRemark(orderRemark);
						tvop2.setUpdateBy(logonUser.getUserId());
						tvop2.setUpdateDate(new Date(System.currentTimeMillis()));
						dao.update(tvop1, tvop2);

						// 更新发运申请表
						TtVsDlvryReqPO tvdrp1 = new TtVsDlvryReqPO();
						TtVsDlvryReqPO tvdrq2 = new TtVsDlvryReqPO();
						tvdrp1.setReqId(new Long(reqId));
						tvdrq2.setReqRemark(orderRemark) ;
						tvdrq2.setFundType(new Long(fundTypeId.split("\\|")[0]));
						if(priceId != null && !priceId.equals("")){
							tvdrq2.setPriceId(new Long(priceId));
						}
						tvdrq2.setOtherPriceReason(otherPriceReason);
						tvdrq2.setModifyFlag(new Integer(modifyFlag));
						tvdrq2.setWarehouseId(new Long(warehouseId));

						// tvdrq2.setReqTotalPrice(new Double(reqTotalPrice));
						tvdrq2.setDiscount(new Double(discountTotalPrice));
						tvdrq2.setReserveTotalAmount(new Integer(reserveTotalAmount));
						tvdrq2.setDeliveryType(new Integer(deliveryType));
						// 发运方式为自提时
						if (tvdrq2.getDeliveryType().intValue() == Constant.TRANSPORT_TYPE_01
								.intValue()) {
							tvdrq2.setReceiver(orderPo.getOrderOrgId());
						}

						tvdrq2.setVer(new Integer(Integer.parseInt(reqVer) + 1));
						tvdrq2.setUpdateBy(logonUser.getUserId());
						tvdrq2.setUpdateDate(new Date(System.currentTimeMillis()));
						/*
						 * if (Integer.parseInt(orderType) ==
						 * Constant.ORDER_TYPE_01.intValue()) {
						 * tvdrq2.setReqTotalAmount(new
						 * Integer(reserveTotalAmount)); }
						 */
						dao.update(tvdrp1, tvdrq2);

						// orderPo = reportDao.getTtSalesOrder(orderId);
						
						ResourceReserveQuery rrq = new ResourceReserveQuery() ;
						Long logId = rrq.insLog(reqId, logonUser.getUserId()) ;

						for (int i = 0; i < detailId.length; i++) {
							if (detailId[i] != null && !detailId[i].equals("")) {
								if (Integer.parseInt(orderType) != Constant.ORDER_TYPE_01.intValue()) {
									// 更新订单明细
									TtVsOrderDetailPO tvodp1 = new TtVsOrderDetailPO();
									TtVsOrderDetailPO tvodp2 = new TtVsOrderDetailPO();
									tvodp1.setDetailId(new Long(orderDetailId[i]));
									
									/*TtVsOrderDetailPO tvodp = reportDao.getTtSalesOrderDetail(orderDetailId[i]);
									tvodp2.setCallAmount(tvodp.getCallAmount()- new Integer(reqAmount[i])+ new Integer(reserveAmount[i]));
								} else {*/
									tvodp2.setCheckAmount(new Integer(reserveAmount[i]));
									
									dao.update(tvodp1, tvodp2);
								}
								

								// 更新发车申请明细
								TtVsDlvryReqDtlPO tvdrdp1 = new TtVsDlvryReqDtlPO();
								TtVsDlvryReqDtlPO tvdrdp2 = new TtVsDlvryReqDtlPO();
								tvdrdp1.setDetailId(new Long(detailId[i]));
								tvdrdp2.setSinglePrice(new Double(singlePrice[i]));
								tvdrdp2.setTotalPrice(new Double(totalPrice[i])
										- new Double(discountPrice[i]));
								tvdrdp2.setDiscountRate(new Float(discountRate[i]));
								tvdrdp2.setDiscountSPrice(new Double(discountSPrice[i]));
								tvdrdp2.setDiscountPrice(new Double(discountPrice[i]));
								tvdrdp2.setReserveAmount(new Integer(reserveAmount[i]));
								tvdrdp2.setUpdateBy(logonUser.getUserId());
								tvdrdp2.setUpdateDate(new Date(System.currentTimeMillis()));
								tvdrdp2.setVer(new Integer(Integer.parseInt(ver[i] == null
										|| ver[i].equals("") ? "0" : ver[i]) + 1));
								/*
								 * if (Integer.parseInt(orderType) ==
								 * Constant.ORDER_TYPE_01 .intValue()) {
								 * tvdrdp2.setReqAmount(new
								 * Integer(reserveAmount[i])); }
								 */
								dao.update(tvdrdp1, tvdrdp2);

								// 删除资源保留
								TtVsOrderResourceReservePO tvorrp = new TtVsOrderResourceReservePO();
								tvorrp.setReqDetailId(new Long(detailId[i]));
								dao.delete(tvorrp);
								
								// 新增资源保留
								if (batchNo[i] != null && !batchNo[i].equals("")) {
									String[] tempArray = batchNo[i].split("\\/");
									for (int j = 0; j < tempArray.length; j++) {
										String tempStr = tempArray[j];
										String[] tempArray2 = tempStr.split("-");
										String patchNO = tempArray2[0];
										String count = tempArray2[1];
										tvorrp = new TtVsOrderResourceReservePO();
										tvorrp.setReserveId(Long.parseLong(SequenceManager.getSequence("")));
										tvorrp.setReqDetailId(new Long(detailId[i]));
										tvorrp.setMaterialId(new Long(materialId[i]));
										tvorrp.setBatchNo(patchNO);
										tvorrp.setAmount(new Integer(count));
										tvorrp.setReserveStatus(Constant.RESOURCE_RESERVE_STATUS_01);
										tvorrp.setOemCompanyId(logonUser.getCompanyId());
										tvorrp.setWarehouseId(new Long(warehouseId));
										tvorrp.setReserveType(Constant.RESERVE_TYPE_01);
										dao.insert(tvorrp);
										
										rrq.insDtlLog(logId, detailId[i], patchNO, count, "0", materialId[i], logonUser.getUserId()) ;
									}
								}
							} else {

								// 新增订单明细
								TtVsOrderDetailPO tvodp = new TtVsOrderDetailPO();
								tvodp.setDetailId(Long.parseLong(SequenceManager.getSequence("")));
								tvodp.setOrderId(new Long(orderId));
								tvodp.setMaterialId(new Long(materialId[i]));
								tvodp.setOrderAmount(new Integer(0));
								tvodp.setCallAmount(new Integer(0));
								tvodp.setCheckAmount(new Integer(reserveAmount[i]));
								tvodp.setSinglePrice(new Double(singlePrice[i]));
								tvodp.setTotalPrice(new Double(totalPrice[i]) - new Double(discountPrice[i]));
								tvodp.setVer(new Integer(0));
								tvodp.setDiscountRate(new Float(discountRate[i]));
								tvodp.setDiscountSPrice(new Double(discountSPrice[i]));
								tvodp.setDiscountPrice(new Double(discountPrice[i]));
								tvodp.setCreateBy(logonUser.getUserId());
								tvodp.setCreateDate(new Date(System.currentTimeMillis()));
								dao.insert(tvodp);

								// 新增发车申请明细
								TtVsDlvryReqDtlPO tvdrdp = new TtVsDlvryReqDtlPO();
								tvdrdp.setDetailId(Long.parseLong(SequenceManager.getSequence("")));
								tvdrdp.setReqId(new Long(reqId));
								tvdrdp.setOrderDetailId(tvodp.getDetailId());
								tvdrdp.setMaterialId(new Long(materialId[i]));
								tvdrdp.setReqAmount(new Integer(0));
								tvdrdp.setVer(new Integer(0));
								tvdrdp.setSinglePrice(new Double(singlePrice[i]));
								tvdrdp.setTotalPrice(new Double(totalPrice[i]) - new Double(discountPrice[i]));
								tvdrdp.setDiscountRate(new Float(discountRate[i]));
								tvdrdp.setDiscountSPrice(new Double(discountSPrice[i]));
								tvdrdp.setDiscountPrice(new Double(discountPrice[i]));
								tvdrdp.setReserveAmount(new Integer(reserveAmount[i]));
								tvdrdp.setCreateBy(logonUser.getUserId());
								tvdrdp.setCreateDate(new Date(System.currentTimeMillis()));
								dao.insert(tvdrdp);

								// 新增资源保留
								if (batchNo[i] != null && !batchNo[i].equals("")) {
									String[] tempArray = batchNo[i].split("\\/");
									for (int j = 0; j < tempArray.length; j++) {
										String tempStr = tempArray[j];
										String[] tempArray2 = tempStr.split("-");
										String patchNO = tempArray2[0];
										String count = tempArray2[1];
										TtVsOrderResourceReservePO tvorrp = new TtVsOrderResourceReservePO();
										tvorrp.setReserveId(Long.parseLong(SequenceManager.getSequence("")));
										tvorrp.setReqDetailId(tvdrdp.getDetailId());
										tvorrp.setMaterialId(new Long(materialId[i]));
										tvorrp.setBatchNo(patchNO);
										tvorrp.setAmount(new Integer(count));
										tvorrp.setReserveStatus(Constant.RESOURCE_RESERVE_STATUS_01);
										tvorrp.setOemCompanyId(logonUser.getCompanyId());
										tvorrp.setWarehouseId(new Long(warehouseId));
										tvorrp.setReserveType(Constant.RESERVE_TYPE_01);
										dao.insert(tvorrp);
										
										rrq.insDtlLog(logId, tvdrdp.getDetailId().toString(), patchNO, count, "0", materialId[i], logonUser.getUserId()) ;
									}
								}
							}
						}

					}

				} else {
					returnValue = "2";
				}
			} else {
				returnValue = "2";
			}
			act.setOutData("returnValue", returnValue);
		} catch(RuntimeException e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.PUTIN_FAILURE_CODE,e.getMessage());
			logger.error(logonUser,e1);
			act.setException(e);
		} catch (Exception e) {// 异常方法
			BizException e1 = new BizException(act, e, ErrorCodeConstant.SAVE_FAILURE_CODE,
					"订单资源审核：保留数量确认");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	/**
	 * 批次号选择
	 */
	public void patchNoSelect() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			String wareHouseId = CommonUtils.checkNull(request.getParamValue("wareHouseId"));// 仓库id
			String materalId = CommonUtils.checkNull(request.getParamValue("materalId")); // 订单数量
			String batchNo = CommonUtils.checkNull(request.getParamValue("batchNo")); // 物料id
			String amount = CommonUtils.checkNull(request.getParamValue("amount")); // 已配车数量
			String orderType = CommonUtils.checkNull(request.getParamValue("orderType")); // 订单类型
			String specialBatchNo = CommonUtils.checkNull(request.getParamValue("specialBatchNo")); // 特殊批次号
			String initNo = CommonUtils.checkNull(request.getParamValue("initNo")); // 初始批次
			String reqAmount = CommonUtils.checkNull(request.getParamValue("reqAmount")); // 申请数量
			Long companyId = logonUser.getCompanyId();

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("wareHouseId", wareHouseId);
			map.put("materalId", materalId);
			map.put("orderType", orderType);
			map.put("specialBatchNo", specialBatchNo);
			map.put("companyId", companyId.toString());

			List<Map<String, Object>> w_List = dao.getPatchNoSelectQuery(map);

			act.setOutData("w_List", w_List);
			act.setOutData("batchNo", batchNo);
			act.setOutData("materalId", materalId);
			act.setOutData("amount", amount);
			act.setOutData("initNo", initNo);
			act.setOutData("reqAmount", reqAmount);
			act.setForword(ORDER_RESOURCE_RESERVE_PATCH_NO_QUERY_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE,
					"订单资源审核批次号选择");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	public void orderResourceReserveCancel() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			String reqVer = request.getParamValue("reqVer");// 发运申请VER
			String orderId = request.getParamValue("orderId"); // 订单ID
			String reqId = request.getParamValue("reqId"); // 发运申请ID
			String orderType = request.getParamValue("orderType"); // 订单类型
			String[] reqAmount = request.getParamValues("reqAmount");// 申请数量
			String[] detailId = request.getParamValues("detailId");// 发运申请明细ID
			String[] orderDetailId = request.getParamValues("orderDetailId");// 订单明细ID
			String isBingcai = CommonUtils.checkNull(request.getParamValue("isBingcai")); //资金类型:1=兵财存货融资; 0=非兵财存货融资 
			
			String returnValue = "1";

			boolean verFlag = true;
			verFlag = VerFlagDao.getVerFlag("TT_VS_DLVRY_REQ", "REQ_ID", reqId, reqVer);
			if (verFlag) {
				// 发运申请更新
				TtVsDlvryReqPO tvdrp1 = new TtVsDlvryReqPO();
				tvdrp1.setReqId(new Long(reqId));
				TtVsDlvryReqPO tvdrp2 = new TtVsDlvryReqPO();
				tvdrp2.setReqStatus(Constant.ORDER_REQ_STATUS_07);// 已取消
				tvdrp2.setVer(new Integer(Integer.parseInt(reqVer) + 1));
				tvdrp2.setUpdateBy(logonUser.getUserId());
				tvdrp2.setUpdateDate(new Date(System.currentTimeMillis()));
				dao.update(tvdrp1, tvdrp2);

				// 释放保留资源
				for (int i = 0; i < detailId.length; i++) {
					if (detailId[i] != null && !detailId.equals("")) {
						TtVsOrderResourceReservePO tvorrp1 = new TtVsOrderResourceReservePO();
						tvorrp1.setReqDetailId(new Long(detailId[i]));
						TtVsOrderResourceReservePO tvorrp2 = new TtVsOrderResourceReservePO();
						tvorrp2.setReserveStatus(Constant.RESOURCE_RESERVE_STATUS_02);// 已取消
						dao.update(tvorrp1, tvorrp2);

						// 常规订单更新订单明细
						if (Integer.parseInt(orderType) == Constant.ORDER_TYPE_01.intValue()) {
							TtVsOrderDetailPO tvodp = reportDao.getTtSalesOrderDetail(orderDetailId[i]);

							TtVsOrderDetailPO tvodp1 = new TtVsOrderDetailPO();
							tvodp1.setDetailId(tvodp.getDetailId());
							TtVsOrderDetailPO tvodp2 = new TtVsOrderDetailPO();
							tvodp2.setCallAmount(tvodp.getCallAmount().intValue() - Integer.parseInt(reqAmount[i]));
							tvodp2.setUpdateBy(logonUser.getUserId());
							tvodp2.setUpdateDate(new Date(System.currentTimeMillis()));
							dao.update(tvodp1, tvodp2) ;
						}
					}
				}

				// 非常规订单更新订单
				if (Integer.parseInt(orderType) != Constant.ORDER_TYPE_01.intValue()) {
					TtVsOrderPO tvop1 = new TtVsOrderPO();
					tvop1.setOrderId(new Long(orderId));
					TtVsOrderPO tvop2 = new TtVsOrderPO();
					tvop2.setOrderStatus(Constant.ORDER_STATUS_06);// 已取消
					tvop2.setUpdateBy(logonUser.getUserId());
					tvop2.setUpdateDate(new Date(System.currentTimeMillis()));
					dao.update(tvop1, tvop2);
				}

				// 释放资金
				if (!"1".equals(isBingcai)) {
					dmsReleasePrice(reqId, logonUser.getUserId().toString());
				}
				/*// 释放资金
				if (!"1".equals(isBingcai)) {
					AccountBalanceDetailDao dao = AccountBalanceDetailDao.getInstance();
					dao.releaseAllFreezeAmountByReqId(reqId, logonUser.getUserId().toString());
				}*/
				
				//向发运申请操作日志表写入日志信息
				ReqLogUtil.creatReqLog(Long.parseLong(reqId), Constant.REQ_LOG_TYPE_09, logonUser.getUserId());
				//同步结算中心下级经销商订单
				AccountBalanceDetailDao.getInstance().syncOrderToDFS(Long.valueOf(orderId), true, logonUser.getUserId(), true);
			} else {
				returnValue = "2";
			}
			act.setOutData("returnValue", returnValue);
		} catch(RuntimeException e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.PUTIN_FAILURE_CODE,e.getMessage());
			logger.error(logonUser,e1);
			act.setException(e);
		} catch (Exception e) {// 异常方法
			BizException e1 = new BizException(act, e, ErrorCodeConstant.SAVE_FAILURE_CODE,
					"订单资源审核保存");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
}
